package com.macro.mall.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dao.MmsDao;
import com.macro.mall.dto.Mms;
import com.macro.mall.dto.MmsExample;
import com.macro.mall.dto.MmsRequest;
import com.macro.mall.service.MmsService;
import com.macro.mall.util.example.ExampleUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author HuangZiy
 * @version 8.0
 * @date 2021/4/26 22:43
 */
@Service
public class MmsServiceImpl implements MmsService {
    @Autowired
    private MmsDao mmsDao;

    @Override
    public CommonResult<List<Mms>> getMms(MmsRequest request) {
        MmsExample mmsExample = ExampleUtil.getExampleByAnnotation(request, MmsExample.class);
        List<Mms> mms = mmsDao.selectByExample(mmsExample);
        if (CollectionUtils.isEmpty(mms)) {
            return CommonResult.success(null);
        }
        return CommonResult.success(mms);
    }

    @Override
    public CommonResult<String> addMms(MmsRequest request) {
        Mms mms = new Mms();
        BeanUtils.copyProperties(request, mms);
        int i = mmsDao.insertSelective(mms);
        if (i <= 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success("插入成功");
    }

    @Override
    public CommonResult<String> updateMms(MmsRequest request) {
        Mms mms = new Mms();
        BeanUtils.copyProperties(request, mms);
        int i = mmsDao.updateByPrimaryKeySelective(mms);
        if (i <= 0) {
            return CommonResult.failed("修改失败");
        }
        return CommonResult.success("修改成功");
    }

    @Override
    public CommonResult<String> deleteMms(Integer id) {
        int i = mmsDao.deleteByPrimaryKey(id);
        if (i <= 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功");
    }
}
